package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbAddressVoService;
import com.dongdongshop.vo.TbAddressVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("TbAddressVo")
public class TbAddressVoController {

    //注入service
    @DubboReference
    private TbAddressVoService tbAddressVoService;

    //查询订单地址数据
    @ResponseBody
    @RequestMapping("showTbAddressVo")
    public Result showTbAddressVo() {
        try {
            //获取当前登录对象
            TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
            List<TbAddressVo> list = tbAddressVoService.showTbAddressVo(tbUser.getUsername());
            return Result.response(ResultEnums.SHOW_SUCCESS).setData(list);
        } catch (Exception e) {
            return Result.response(ResultEnums.SHOW_ERROR);
        }
    }

    //获取省份地址
    @ResponseBody
    @RequestMapping("showTbProvinces")
    public Result showTbProvinces() {
        try {
            List<TbProvinces> list = tbAddressVoService.showTbProvinces();
            return Result.response(ResultEnums.SHOW_SUCCESS).setData(list);
        } catch (Exception e) {
            return Result.response(ResultEnums.SHOW_ERROR);
        }
    }

    //获取市区地址
    @ResponseBody
    @RequestMapping("showTbCities")
    public Result showTbCities(String provinceid) {
        try {
            List<TbCities> list = tbAddressVoService.showTbCities(provinceid);
            return Result.response(ResultEnums.SHOW_SUCCESS).setData(list);
        } catch (Exception e) {
            return Result.response(ResultEnums.SHOW_ERROR);
        }
    }

    //获取县地址
    @ResponseBody
    @RequestMapping("showTbAreas")
    public Result showTbAreas(String cityid) {
        try {
            List<TbAreas> list = tbAddressVoService.showTbAreas(cityid);
            return Result.response(ResultEnums.SHOW_SUCCESS).setData(list);
        } catch (Exception e) {
            return Result.response(ResultEnums.SHOW_ERROR);
        }
    }

    //添加地址
    @ResponseBody
    @RequestMapping("addTbAddress")
    public Result addTbAddress(TbAddress tbAddress) {
        try {
            TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
            tbAddress.setUserId(tbUser.getUsername());
            tbAddressVoService.addTbAddress(tbAddress);
            return Result.response(ResultEnums.ADD_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.ADD_ERROR);
        }
    }

    //地址回显
    @ResponseBody
    @RequestMapping("echoTbAddress")
    public Result echoTbAddress(Long id) {
        try {
            TbAddress tbAddress = tbAddressVoService.echoTbAddress(id);
            return Result.response(ResultEnums.ADD_SUCCESS).setData(tbAddress);
        } catch (Exception e) {
            return Result.response(ResultEnums.ADD_ERROR);
        }
    }

    //修改地址
    @ResponseBody
    @RequestMapping("updateTbAddress")
    public Result updateTbAddress(TbAddress tbAddress) {
        try {
            TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
            tbAddress.setUserId(tbUser.getUsername());
            tbAddressVoService.updateTbAddress(tbAddress);
            return Result.response(ResultEnums.UPDATE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.UPDATE_ERROR);
        }
    }

    //删除地址
    @ResponseBody
    @RequestMapping("deleteTbAddress")
    public Result deleteTbAddress(Long id) {
        try {
            tbAddressVoService.deleteTbAddress(id);
            return Result.response(ResultEnums.DELETE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.DELETE_ERROR);
        }
    }

    //设置为默认地址
    @ResponseBody
    @RequestMapping("zheweimoren")
    public Result zheweimoren(Long id) {
        try {
            TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
            tbAddressVoService.zheweimoren(id,tbUser);
            return Result.OK();
        } catch (Exception e) {
            System.out.println(e);
            return Result.ER();
        }
    }

}
